﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.Models;
using Enterprise.Management.Caches;
using System.Web.Routing;
using Enterprise.Management.Business;

namespace Enterprise.Management.FilterAttributes
{
    public class LoginAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            LoginBLL bll = new LoginBLL();
            bll.Load();
            if (!bll.Logged)
            {
                if (!string.IsNullOrEmpty(filterContext.HttpContext.Request["ajax"]))
                {
                    HttpContext.Current.Response.StatusCode = 999;
                    filterContext.Result = new EmptyResult();
                }
                else
                {
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                        {
                            controller = "Account",
                            action = "LogOn"
                        }));
                }
            }
        }
    }
}